﻿using System;
using System.Collections.Generic;
using System.Text;

namespace iTunes
{
    class Comm
    {
        private System.IO.Ports.SerialPort serialPort;


        public Comm(System.IO.Ports.SerialPort sp)
        {
            this.serialPort = sp;
            this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPortDataReceived);
        }


        public Comm()
        {
            this.serialPort = new System.IO.Ports.SerialPort("COM1", 2400, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
            serialPort.Handshake = System.IO.Ports.Handshake.None; 
            this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPortDataReceived);
        }

        private void traitement(string data) 
        { 
            textBox1.Text = data.Trim();
        } 

        //fonction appellée lors de la réception de données
        //sensée remplir les tableaux tabIR1 et tabIR2
        //a chaque donnée reçue, on la met dans un tableau de l'infrarouge correspondant

        private void serialPortDataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            //lit ce qu'il y a sur le port COM1 
            try
            {
                RxString = serialPort1.ReadLine();
            }
            catch (System.IO.IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
            int n;

            //result resultat du tryparse
            //si true parsage a ete fait correctement
            bool result = int.TryParse(RxString, out n);

            if (result)
            {
                //Console.WriteLine("Converted '{0}' to {1}.", RxString, n);
                RxString = n.ToString();
                //si la valeur est 0
                //on change la valeur du bouton qui controle le coverflow
                if (RxString.Equals("0"))
                {
                    if (eng.getButonCoverFlow()) eng.setButonCoverFlow(false);
                    else eng.setButonCoverFlow(true);
                }

                // si la valeur est celle de IR1
                else if (RxString.StartsWith("1"))
                {
                    //n contient l'entier
                    //on enleve le 1
                    if (n <= 19) n = n - 10;
                    else if (n <= 199) n = n - 100;
                    else if (n <= 1999) n = n - 1000;
                    else if (n <= 19999) n = n - 10000;

                    eng.getIR1().getTab().Add(n);

                    //graphing
                    Graphics objGraphic = this.pictureBox1.CreateGraphics();
                    Pen pen = new Pen(Color.Black);
                    Rectangle rec = new Rectangle();
                    rec.Width = pictureBox1.Size.Width;
                    rec.Height = pictureBox1.Size.Height;
                    objGraphic.FillRectangle(Brushes.Black, rec);

                    rec.Height = pictureBox1.Size.Height - n * pictureBox1.Size.Height / 1002;
                    objGraphic.FillRectangle(Brushes.White, rec);

                    RxString = "ajout ds tab de IR1 de " + n;
                }
                // si la valeur est celle de IR2
                else if (RxString.StartsWith("2"))
                {
                    //n contient l'entier
                    //on enleve le 2
                    if (n <= 29) n = n - 20;
                    else if (n <= 299) n = n - 200;
                    else if (n <= 2999) n = n - 2000;
                    else if (n <= 29999) n = n - 20000;

                    eng.getIR2().getTab().Add(n);

                    // graphing
                    Graphics objGraphic = this.pictureBox2.CreateGraphics();
                    Pen pen = new Pen(Color.Black);
                    Rectangle rec = new Rectangle();
                    rec.Width = pictureBox1.Size.Width;
                    rec.Height = pictureBox1.Size.Height;
                    objGraphic.FillRectangle(Brushes.Black, rec);

                    rec.Height = pictureBox1.Size.Height - n * pictureBox1.Size.Height / 1002;
                    objGraphic.FillRectangle(Brushes.White, rec);

                    RxString = "ajout ds tab de IR2 de " + n;
                }
                else
                {
                    RxString = "Valeure non conforme";
                }
            }
            else
            {
                //RxString = "ERROR PARSING";
            }
            this.Invoke(new EventHandler(DisplayText));
        }
        
        private void sendCommand(String s)
        {
            // Makes sure serial port is open before trying to write
            try
            {
                if (!(_serialPort.IsOpen))
                    serialPort.Open();
                serialPort.Write(s);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
            }
        }
    
    }
}
